<template>
  <el-container class="home_container">
    <!-- 顶部 Header 区域 -->
    <el-header>
      <div class="home_title">滑雪场后台管理系统</div>
      <div class="home_userinfoContainer">
        <!-- 用户头像 -->
        <el-avatar :src="imgUrl"></el-avatar>

        <!-- 用户名下拉菜单 -->
        <el-dropdown style="position: relative; top: -10px; left: 10px;">
          <span class="el-dropdown-link home_userinfo">
            {{ admin.name }}
            <i class="el-icon-arrow-down el-icon--right home_userinfo"></i>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <!-- 页面主体结构 -->
    <el-container>
      <!-- 侧边栏菜单 -->
      <el-aside width="200px" style="background-color: #a5e7f0">
        <el-menu router>
          <el-menu-item index="adminManage">
            <i class="el-icon-s-unfold"></i> 管理员管理
          </el-menu-item>
          <el-menu-item index="userManage">
            <i class="el-icon-circle-plus"></i> 用户管理
          </el-menu-item>
          <el-menu-item index="equipmentManage">
            <i class="el-icon-s-data"></i> 滑雪设备管理
          </el-menu-item>
          <el-menu-item index="trackManage">
            <i class="el-icon-s-marketing"></i> 滑雪赛道管理
          </el-menu-item>
          <el-menu-item index="coachManage">
            <i class="el-icon-s-help"></i> 教练管理
          </el-menu-item>
          <el-menu-item index="noticeManage">
            <i class="el-icon-goods"></i> 公告栏管理
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主体 + Footer -->
      <el-container>
        <el-main>
          <!-- 面包屑导航 -->
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentRouteName }}</el-breadcrumb-item>
          </el-breadcrumb>
          <!-- 子路由渲染 -->
          <router-view></router-view>
        </el-main>
        <el-footer>@V1.0-Spring Boot + Vue</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script setup>
import { onMounted, ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const route = useRoute()

// 当前登录管理员信息
const admin = ref({})

// 用户头像链接
const imgUrl = ref('https://img1.baidu.com/it/u=3981550796,2434119925&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=800')

// 页面加载时从 localStorage 获取用户信息
onMounted(() => {
  const adminStr = localStorage.getItem('admin')
  if (adminStr) {
    admin.value = JSON.parse(adminStr)
  } else {
    router.replace('/login') // 如果没登录信息，跳转到登录页
  }
})

// 获取当前路由名称用于面包屑
const currentRouteName = computed(() => route.name)

// 注销登录方法
const logout = () => {
  ElMessageBox.confirm('注销登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    localStorage.removeItem('admin')
    router.replace('/login')
  })
}
</script>

<style scoped>
.home_container {
  height: 100%;
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
}

.el-header {
  background-color: #20a0ff;
  color: #333;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.el-aside {
  background-color: #ECECEC;
}

.el-main {
  background-color: #fff;
  color: #000;
  text-align: center;
}

.el-footer {
  background-color: #ea7e53;
  color: #fff;
  font-size: 22px;
  line-height: 60px;
}

.home_title {
  color: #fff;
  font-size: 22px;
  display: inline;
}

.home_userinfo {
  color: #fff;
  cursor: pointer;
}

.home_userinfoContainer {
  display: inline;
  margin-right: 20px;
}
</style>
